
let indexTips;
let $table;
layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate'], function (exports) {
    var $ = layui.$;
    var table = layui.table;
    $table = table;
    var laypage = layui.laypage;
    var laydate = layui.laydate;
    var form = layui.form;
    zmnLayUtil.layFormVerify(form);
    var cols = [
        [
            {field: 'activityId', type: 'checkbox', fixed: 'left', align: 'center'},
            {field: 'activityId', title: '活动ID', align: 'center', unresize: true, sort: false},
            {field: 'name', title: '活动名称', align: 'center', unresize: true, sort: false},
            {field: 'showName', title: '界面显示名称', align: 'center', unresize: true, sort: false},
            {field: 'startTime', title: '开始时间', align: 'center', unresize: true, sort: false, templet :function (row){
                    return zmnUtil.createTime(row.startTime);
                }},
            {field: 'endTime', title: '结束时间', align: 'center', unresize: true, sort: false, templet :function (row){
                    return zmnUtil.createTime(row.endTime);
                }},
            {field: 'creater', title: '创建人', align: 'center', unresize: true, sort: false},
            {field: 'statusDesc', title: '状态', align: 'center', unresize: true, sort: false},
            {field: 'createTime', title: '创建时间', templet: '#createTime', align: 'center', unresize: true,
                templet :function (row){
                    return zmnUtil.createDate(row.createTime);
                }
            },
            {field: 'right', title: '操作', width: 100, toolbar: '#barDemo'}
        ]
    ];
    table.render({
        elem: "#zmn-activity-table",
        toolbar: "#zmn-activity-table-tool",
        title: "活动配置管理",
        data: json.items,
        limit: json.pageSize,
        cols: cols,
        page: true,
        height: 'full-120',
        defaultToolbar: ['filter']
    });

    laypage.render(zmnPage.laypageOptions(json));

    /**
     * 描述: 表单初始化
     **/
    form.render();

    /**
     * 描述: 表单提交事件
     **/
    form.on('submit(query-submit)', function (obj) {
        // paramQuery = obj.field;
        return true;
    });

    // 选择（全选或单选）
    var ids = new Array();
    table.on("checkbox(zmn-activity-table)", function () {
        ids = zmnPage.layIdsChecked(json.items, "activityId");
    });

    /**
     * 启用
     **/
    $(document).on('click', '#btn-activity-enable ', function () {
        if(ids.length <= 0){
            var enable_index = layer.confirm('请先选择!', {
                    btn: ['确定'] //按钮
                },function(){
                    layer.close(enable_index);
                    return ;
                });
            return ;
        }
        let data = {activityIds: ids};
        layer.confirm('启用所选活动？', {
            btn: ['确定'] //按钮
        }, function(){
            $.ajax({
                async: false,
                type: "POST",
                url: "/activity/config/enable.action",
                data: JSON.stringify(data),
                dataType: "json",
                contentType: 'application/json',
                success: function (json) {
                    if (json.status == 200) {
                        //刷新页面
                        ids = new Array();
                        endCallback();
                    }else{
                        layer.msg(json.message, {icon: 5, shift: 6, time: 5000});
                    }
                }
            });
        });
    });
    /**
     *  停用
     **/
    $(document).on('click', '#btn-activity-disable ', function () {
        if(ids.length <= 0){
            var disable_index = layer.confirm('请先选择!', {
                btn: ['确定'] //按钮
            },function(){
                layer.close(disable_index);
                return ;
            });
            return ;
        }
        let data = {activityIds: ids};
        layer.confirm('停用所选活动？', {
            btn: ['确定'] //按钮
        }, function(){
            $.ajax({
                async: false,
                type: "POST",
                url: "/activity/config/disable.action",
                data: JSON.stringify(data),
                dataType: "json",
                contentType: 'application/json',
                success: function (json) {
                    if (json.status == 200) {
                        //刷新页面
                        ids = new Array();
                        endCallback();
                    }else if(json.status == 201){
                        var message = json.message;
                        var disable_index = layer.confirm(message, {
                            btn: ['确定'] //按钮
                        },function(){
                            layer.close(disable_index);
                            forceDisable(data);
                            return ;
                        });
                        return ;
                    }else{
                        layer.msg(json.message, {icon: 5, shift: 6});
                    }
                }
            });
        });
    });

    function forceDisable(data){
        $.ajax({
            async: false,
            type: "POST",
            url: "/activity/config/force-disable.action",
            data: JSON.stringify(data),
            dataType: "json",
            contentType: 'application/json',
            success: function (json) {
                if (json.status == 200) {
                    //刷新页面
                    ids = new Array();
                    endCallback();
                } else {
                    layer.msg(json.message, {icon: 5, shift: 6});
                }
            }
        });
    }

    /**
     * 新增活动
     **/
    $(document).on('click', '#btn-activity-add ', function () {
        let target = "title-act-base";
        let isAdd = true;
        openFrame(null, target, isAdd, false);
    });


    /**
     * 描述: 时间初始化
     **/
    laydate.render({
        elem: '#act-createDate',
    });

    function loadMasterMenus(row) {
        let html = "";
        if (row.status == 1) {
            html += "<div class=\"layui-col-md6 layui-col-sm4\" style='line-height:0'>\n" +
                "            <div class=\"layui-form-item layui-form-item-sm\">\n" +
                "                <button class=\"layui-btn layui-btn-sm layui-btn-normal\" onclick='editBase("+ JSON.stringify(row).replace(/"/g, '&quot;') + ");'>修改基本配置</button>\n" +
                "            </div>&nbsp;\n" +
                "        </div>";
            html += "<div class=\"layui-col-md6 layui-col-sm4\" style='line-height:0'>\n" +
                "            <div class=\"layui-form-item layui-form-item-sm\">\n" +
                "                <button class=\"layui-btn layui-btn-sm layui-btn-normal\" onclick='editDescription("+ JSON.stringify(row).replace(/"/g, '&quot;') + ");'>活动描述配置</button>\n" +
                "            </div>&nbsp;\n" +
                "        </div>";
            html += "<div class=\"layui-col-md6 layui-col-sm4\" style='line-height:0'>\n" +
                "            <div class=\"layui-form-item layui-form-item-sm\">\n" +
                "                <button class=\"layui-btn layui-btn-sm layui-btn-normal\" onclick='editCity("+ JSON.stringify(row).replace(/"/g, '&quot;') + ");'>范围城市配置</button>\n" +
                "            </div>&nbsp;\n" +
                "        </div>";
            html += "<div class=\"layui-col-md6 layui-col-sm4\" style='line-height:0'>\n" +
                "            <div class=\"layui-form-item layui-form-item-sm\">\n" +
                "                <button class=\"layui-btn layui-btn-sm layui-btn-normal\" onclick='editProduct("+ JSON.stringify(row).replace(/"/g, '&quot;') + ");'>产品关联配置</button>\n" +
                "            </div>&nbsp;\n" +
                "        </div>";
            html += "<div class=\"layui-col-md6 layui-col-sm4\" style='line-height:0'>\n" +
                "            <div class=\"layui-form-item layui-form-item-sm\">\n" +
                "                <button class=\"layui-btn layui-btn-sm layui-btn-normal\" onclick='editCoupBatch("+ JSON.stringify(row).replace(/"/g, '&quot;') + ");'>优惠批次配置</button>\n" +
                "            </div>&nbsp;\n" +
                "        </div>";
            html += "<div class=\"layui-col-md6 layui-col-sm4\" style='line-height:0'>\n" +
                "            <div class=\"layui-form-item layui-form-item-sm\">\n" +
                "                <button class=\"layui-btn layui-btn-sm layui-btn-normal\" onclick='showActUrl("+ JSON.stringify(row).replace(/"/g, '&quot;') + ");'>链接详情</button>\n" +
                "            </div>&nbsp;\n" +
                "        </div>";
        }else{
            html += "<div class=\"layui-col-md6 layui-col-sm4\" style='line-height:0'>\n" +
                "            <div class=\"layui-form-item layui-form-item-sm\">\n" +
                "                <button class=\"layui-btn layui-btn-sm layui-btn-disabled layui-btn-normal\" disabled='true'>修改基本配置</button>\n" +
                "            </div>&nbsp;\n" +
                "        </div>";

            html +=  "<div class=\"layui-col-md6 layui-col-sm4\" style='line-height:0'>\n" +
                "            <div class=\"layui-form-item layui-form-item-sm\">\n" +
                "                <button class=\"layui-btn layui-btn-sm layui-btn-normal\" onclick='onlyEditDescription("+ JSON.stringify(row).replace(/"/g, '&quot;') + ");'>活动描述配置</button>\n" +
                "            </div>&nbsp;\n" +
                "        </div>";
            html += "<div class=\"layui-col-md6 layui-col-sm4\" style='line-height:0'>\n" +
                "            <div class=\"layui-form-item layui-form-item-sm\">\n" +
                "                <button class=\"layui-btn layui-btn-sm layui-btn-disabled layui-btn-normal\" disabled='true'>范围城市配置</button>\n" +
                "            </div>&nbsp;\n" +
                "        </div>";
            html += "<div class=\"layui-col-md6 layui-col-sm4\" style='line-height:0'>\n" +
                "            <div class=\"layui-form-item layui-form-item-sm\">\n" +
                "                <button class=\"layui-btn layui-btn-sm layui-btn-disabled layui-btn-normal\" disabled='true'>产品关联配置</button>\n" +
                "            </div>&nbsp;\n" +
                "        </div>";
            html += "<div class=\"layui-col-md6 layui-col-sm4\" style='line-height:0'>\n" +
                "            <div class=\"layui-form-item layui-form-item-sm\">\n" +
                "                <button class=\"layui-btn layui-btn-sm layui-btn-disabled layui-btn-normal\" disabled='true'>优惠批次配置</button>\n" +
                "            </div>&nbsp;\n" +
                "        </div>";
            html += "<div class=\"layui-col-md6 layui-col-sm4\" style='line-height:0'>\n" +
                "            <div class=\"layui-form-item layui-form-item-sm\">\n" +
                "                <button class=\"layui-btn layui-btn-sm layui-btn-normal\" onclick='showDetail("+ JSON.stringify(row).replace(/"/g, '&quot;') + ");'>活动详情</button>\n" +
                "            </div>&nbsp;\n" +
                "        </div>";
            html += "<div class=\"layui-col-md6 layui-col-sm4\" style='line-height:0'>\n" +
                "            <div class=\"layui-form-item layui-form-item-sm\">\n" +
                "                <button class=\"layui-btn layui-btn-sm layui-btn-normal\" onclick='showActUrlNoUpdate("+ JSON.stringify(row).replace(/"/g, '&quot;') + ");'>链接详情</button>\n" +
                "            </div>&nbsp;\n" +
                "        </div>";
        }

        return html;
    }

    function openMenuDiv(activityId) {
        let selector = '#showMenu-' + activityId;
        indexTips = layer.tips($("#toolBar").html(), selector, {
            shade: [0],
            shadeClose: true,
            area: ['230px', '190px'],
            time: 0, //20秒后自动关闭
            tips: [3, '#FFFFFF'],
        });
    }
    //监听行工具事件
    table.on('tool(zmn-activity-table)', function (obj) {
        if (obj.event === 'operation') {
            var row = obj.data;
            $("#toolBar").empty();
            $("#toolBar").append('<div class="layui-btn-container">' + loadMasterMenus(eval(row)) + '</div>');
            openMenuDiv(obj.data.activityId);
        }
    });

    //对外暴露的接口
    exports('zmn_plat_activity_list', {});
});
function showDetail(row){
    let target = "title-act-base";
    let btnDisable = 1;
    openFrame(row.activityId, target, 0, btnDisable);
}
function editBase(row){
    let target = "title-act-base";
    openFrame(row.activityId, target);
}

function editDescription(row){
    let target = "title-act-description";
    openFrame(row.activityId, target);
}

function editCity(row){
    let target = "title-act-city";
    openFrame(row.activityId, target);
}

function editProduct(row){
    let target = "title-act-product";
    openFrame(row.activityId, target);
}

function onlyEditDescription(row){
    let target = "title-act-description";
    let btnDisable = 1;
    openFrame(row.activityId, target, 0, btnDisable);
}

function editCoupBatch(row){
    let target = "title-act-coup-batch";
    openFrame(row.activityId, target);
}
function showActUrl(row){
    let target = "title-act-url";
    openFrame(row.activityId, target);
}

function showActUrlNoUpdate(row){
    let target = "title-act-url";
    let btnDisable = 1;
    openFrame(row.activityId, target, 0, btnDisable);
}

function openFrame(activityId, target, isAdd, btnDisable){
    layer.close(indexTips);
    var url = "/activity/config/modify.action?1=1";
    if(activityId){
        url = url + "&activityId=" + activityId;
    }
    if(isAdd){
        url = url + "&isAdd=1";
    }
    if(btnDisable){
        url = url + "&btnDisable=1";
    }
    if(target){
        url = url + "#target=" + target;
    }
    // console.info(url)
    //let title = "活动配置";
    zmnLayUtil.openIframeCustom(layer, url, {shade: 1,area: ['100%', '100%'], end: endCallback});
}
//窗口关闭回调
function endCallback(){
    layui.$("#query-submit").click();
}